package br.edu.unicentro.mymib.mbean;

import javax.faces.event.ActionEvent;

import br.edu.unicentro.mymib.base.EntityManagerService;
import br.edu.unicentro.mymib.entity.Usuario;

public class RegisterMBean {

	private boolean windowVisible = false;
	private String nome;
	private String email;
	private String senha;

	public RegisterMBean() {

	}

	public void exibirWindow(ActionEvent event) {
		this.windowVisible = true;
	}

	public void fecharWindow(ActionEvent event) {
		this.windowVisible = false;
		clear();
	}

	public void cadastrar(ActionEvent event) {
		Usuario u = new Usuario();
		if (nome != null && !nome.equals("")) {
			u.setNome(nome);
		}

		if (email != null && !email.equals("")) {
			u.setEmail(email);
		}

		if (senha != null && !senha.equals("")) {
			u.setSenha(senha);
		}
		try {
			u = (Usuario) EntityManagerService.salvar(u);
			clear();
			this.windowVisible = false;
		} catch (Exception e) {
			//TODO tratar, exibir que houve erro
			e.printStackTrace();
		}
		clear();
		
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public boolean isWindowVisible() {
		return windowVisible;
	}

	public void clear() {
		this.email = "";
		this.nome = "";
		this.senha = "";
	}

}
